using System;
using System.Collections.Generic;
using System.Text;

namespace AsyncAwaitDemo
{
    public class ZString
    {
        public static string Convert(string s, int numRows) 
        {
            if (numRows <=1)
            {
                return s;
            }
            
            
            List<StringBuilder> rows = new List<StringBuilder>();//行集合
            
            //计算出有多少行
            for (int i = 0; i < Math.Min(numRows, s.Length); i++)
            {
                rows.Add(new StringBuilder()); 
            }

            int curRow = 0;//记录移动行

            bool isDown = false; //是否向下移动

            //遍历字符串 存储到数组中
            for (int i = 0; i < s.Length; i++)
            {
                rows[curRow].Append(s[i]);
                if (curRow == 0 || curRow == rows.Count-1) //当处于最上和最下的时候切换方向
                {
                    isDown = !isDown;
                }
            
                curRow += isDown ? 1 : -1;
            }
            
            //读取新字符串

            string txt = "";

            for (int i = 0; i < rows.Count; i++)
            {
                txt += rows[i].ToString();
            }

            return txt;

        }
    }
}